import Axios from "axios";
import qs from "qs";

/**
 * post请求
 * @param action
 * @param param
 * @param set_url
 * @method post OR get
 * @returns {Promise<AxiosResponse<T>>}
 */
export const post = function (action, param, set_url) {
    let actions = action.split('/')
    let url = set_url ? set_url : document.getElementById("louie").getAttribute("data-url");
    let params = {
        pages: actions[0],
        doing: actions[1]
    }
    let send_data = Object.assign(params, param)
    return Axios.post(url, qs.stringify(send_data))
}

/**
 * get 请求
 * @param action
 * @param param
 * @param set_url
 * @returns {Promise<AxiosResponse<T>>}
 */
export const get = function (action, param, set_url) {
    let actions = action.split('/')
    let url = set_url ? set_url : document.getElementById("louie").getAttribute("data-url");
    let params = {
        pages: actions[0],
        doing: actions[1]
    }
    let send_data = Object.assign(params, param)
    for (let key in send_data) {
        if (send_data[key]) {
            url += '&' + key +'=' + send_data[key]
        }
    }
    return Axios.get(url)
}

/**
 * 上传文件
 * @param data
 * @returns {Promise<AxiosResponse<T>>}
 */
export const upload = function(data) {
    let url = document.getElementById("louie").getAttribute("data-url");
    return Axios.post(url, data, {
        headers: {'Content-Type': 'multipart/form-data'}
    });
}
